/*
 * ServerNetworkInterface.java
 *
 * Created on December 20, 2006, 7:47 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package net.sf.iSnake.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import net.sf.iSnake.aimodule.IClientNetworkInterface;

/**
 *This class receives client and acts as a mediator between the client and the server.
 *It helps the server to communicate with the client(as invoked by server)
 * @author Suraj
 */
public class ServerNetworkInterface extends Thread {
    private boolean stop=false;
    private ServerCorePre ServerPre;
    private ServerCorePost ServerPost;
    private int port=9669;
    private int MAX_CLIENT=ServerCorePre.MAX_CLIENT;
    private ServerSocket server;
    private ClientListener Client[]=new ClientListener[MAX_CLIENT];
    
    private IClientNetworkInterface imJ;
    
    /** Creates a new instance of ServerNetworkInterface
     */
    public ServerNetworkInterface() {
    }
    
    /** Creates a new instance of ServerNetworkInterface with arguments
     *@param ServerPre The Pre-session Server Object
     *@param ServerPost The Post-session Server Object
     *@param port The Port no at which the Server Should be created
     */
    public ServerNetworkInterface(ServerCorePre ServerPre,ServerCorePost ServerPost,int port){
        this.port=port;
        this.ServerPre=ServerPre;
        this.ServerPost=ServerPost;
        this.start();
    }
    
    /** Sends the string str to the Specified client
     *@param obj The Object to be send
     */
    public void send(Object obj){
        for(int i=1;i<MAX_CLIENT;i++){
            try{
                if(!Client[i].isStop()){
                    Client[i].send(obj);
                }
            } catch(NullPointerException ex){}
        }
    }
    
    /** Sends the string str to the Specified client
     *@param no The client no. to whom the Object is to be send
     *@param obj The Object to be send
     */
    public boolean send(short no,Object obj){
        try{
            Client[no].send(obj);
        } catch(NullPointerException ex){return false;}
        return true;
    }
    /** This is actually a thread that waits the client to connect to the server */
    public void run(){
        try{
            server=new ServerSocket(port);
            ServerPre.status(ServerStatus.DEBUG_INFO,"Server Started...[Server] "+server.getInetAddress().getLocalHost()+"  [Port] "+server.getLocalPort());
            //imJ=new IClientNetworkInterface(9669);
        } catch(IOException ex){
            ServerPre.status(ServerStatus.DEBUG_INFO,"Error Startng Server... [Port] "+port);
        }
        Socket soc;
        while (!stop) {
            try {
                soc=server.accept();
                if(!stop){
                    int no=ServerPre.getAvailable();
                    if(no!=-1){
                        ServerPre.status(ServerStatus.DEBUG_INFO,"New Client Joined assigned with ID:"+no);
                        Client[no]=new ClientListener(ServerPre,ServerPost,no,soc);
                    }
                } else soc.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    /**Throws the specified Client
     *@param no The Client No which is to be thrown
     */
    public void closeClient(int no){
        Client[no].close();
        Client[no]=null;
    }
    /** Stop this thread safely after starting the game.
     */
    public void close(){
        stop=true;
    }
}
